from LR import *
from delete import *
from insert import *
from MainFunction import *
from MainFunction import *
from Register import *
from revise import *
from select import *
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidgetItem
import sqlite3
from tkinter import messagebox


class Log(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Log()
        self.ui.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)  # 去除多余界面
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)  # 去除多余界面
        self.shadow = QtWidgets.QGraphicsDropShadowEffect(self)
        self.shadow.setOffset(0, 0)
        self.shadow.setBlurRadius(15)
        self.shadow.setColor(QtCore.Qt.black)
        self.ui.label_2.setGraphicsEffect(self.shadow)  # 背景加阴影
        self.ui.pushButton.clicked.connect(self.MainFunction)  # 根据MainFunction函数跳转到MainFunction
        self.ui.pushButton_3.clicked.connect(self.Register)  # 根据Register函数跳转到Register
        self.show()

    def MainFunction(self):
        account = self.ui.lineEdit.text()
        password = self.ui.lineEdit_2.text()
        if account == '闲泽' and password == '9050':
            self.win = FunctionWindow()
            self.close()
        else:
            messagebox.showerror("错，门神降临！", "我的页面没了，你的页面给我，好吗！")

    def Register(self):
        self.win = RegisterWindow()
        self.close()


class RegisterWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Reg()
        self.ui.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.shadow = QtWidgets.QGraphicsDropShadowEffect(self)
        self.shadow.setOffset(0, 0)
        self.shadow.setBlurRadius(15)
        self.shadow.setColor(QtCore.Qt.black)
        self.ui.pushButton_3.clicked.connect(self.back_log)
        self.ui.pushButton.clicked.connect(self.clear)
        self.show()

    def back_log(self):
        self.win = Log()
        self.close()

    def clear(self):
        self.ui.lineEdit.clear()
        self.ui.lineEdit_2.clear()
        self.ui.lineEdit_3.clear()


class FunctionWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Function()
        self.ui.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.shadow = QtWidgets.QGraphicsDropShadowEffect(self)
        self.shadow.setOffset(0, 0)
        self.shadow.setBlurRadius(15)
        self.shadow.setColor(QtCore.Qt.black)
        self.ui.pushButton_4.clicked.connect(self.delete)  # 根据delete函数跳转到delete界面
        self.ui.pushButton_2.clicked.connect(self.insert)  # 根据insert函数跳转到insert界面
        self.ui.pushButton_3.clicked.connect(self.revise)  # 根据revise函数跳转到revise界面
        self.ui.pushButton.clicked.connect(self.select)  # 根据select函数跳转到select界面
        self.ui.pushButton_7.clicked.connect(self.show_data)
        self.ui.pushButton_8.clicked.connect(self.clear)
        self.show()

    def show_data(self):
        # 从数据库中检索书籍信息
        conn = sqlite3.connect('./database/student.db')  # 必须使用绝对路径
        cursor = conn.cursor()
        cursor.execute('SELECT * FROM score')
        rows = cursor.fetchall()
        """添加数据"""
        for row_number, row in enumerate(rows):
            self.ui.tableWidget.insertRow(row_number)  # row_number+1:给表头留一行，从第2行添加
            for column_number, data in enumerate(row):
                self.ui.tableWidget.setItem(row_number, column_number, QTableWidgetItem(str(data)))

    def clear(self):
        self.ui.tableWidget.clearContents()

    def delete(self):
        self.win = Delete()

    def insert(self):
        self.win = Insert()

    def revise(self):
        self.win = Revise()

    def select(self):
        self.win = Select()


class Delete(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Delete()
        self.ui.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.ui.pushButton.clicked.connect(self.dele_fetchone)
        self.show()

    def dele_fetchone(self):
        search_id = self.ui.lineEdit.text()
        conn = sqlite3.connect('./database/student.db')  # 必须使用绝对路径
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM score WHERE 学号=?", (search_id,))
        row = cursor.fetchone()
        if row:
            cursor.execute("DELETE FROM score WHERE 学号=?", (search_id,))
            conn.commit()
            concent = "该学生已删除"
            self.ui.textEdit.append(str(concent))
            self.ui.lineEdit.clear()
        else:
            messagebox.showerror("该记录不存在！", "此处无记录，慢走不送，哼！")


class Insert(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Insert()
        self.ui.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.ui.pushButton.clicked.connect(self.add_student)
        self.show()

    def add_student(self):
        Ids = self.ui.lineEdit.text()
        Names = self.ui.lineEdit_2.text()
        Englishs = self.ui.lineEdit_3.text()
        Maths = self.ui.lineEdit_4.text()
        Computers = self.ui.lineEdit_5.text()
        Totals = self.ui.lineEdit_6.text()
        conn = sqlite3.connect('./database/student.db')  # 必须使用绝对路径
        cursor = conn.cursor()
        cursor.execute(
            "INSERT INTO score (学号, 姓名, 英语, 数学, 计算机, 总分) VALUES (?, ?, ?, ?, ?, ?)",
            (Ids, Names, Englishs, Maths, Computers, Totals))
        conn.commit()
        self.ui.lineEdit.clear()
        self.ui.lineEdit_2.clear()
        self.ui.lineEdit_3.clear()
        self.ui.lineEdit_4.clear()
        self.ui.lineEdit_5.clear()
        self.ui.lineEdit_6.clear()


class Revise(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Revise()
        self.ui.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.ui.pushButton.clicked.connect(self.revise_data)
        self.show()

    def revise_data(self):
        select_id = self.ui.lineEdit_6.text()
        up_id = self.ui.lineEdit.text()
        up_name = self.ui.lineEdit_3.text()
        up_english = self.ui.lineEdit_2.text()
        up_math = self.ui.lineEdit_4.text()
        up_computer = self.ui.lineEdit_5.text()
        up_total = self.ui.lineEdit_7.text()
        # 从数据库中检索书籍信息
        conn = sqlite3.connect('./database/student.db')  # 必须使用绝对路径
        cursor = conn.cursor()
        cursor.execute(
            "UPDATE score SET 学号=?, 姓名=?, 英语=?, 数学=?, 计算机=?, 总分=? WHERE 学号=?",
            (up_id, up_name, up_english, up_math, up_computer, up_total, select_id))
        conn.commit()
        self.ui.lineEdit.clear()
        self.ui.lineEdit_2.clear()
        self.ui.lineEdit_3.clear()
        self.ui.lineEdit_4.clear()
        self.ui.lineEdit_5.clear()
        self.ui.lineEdit_6.clear()
        self.ui.lineEdit_7.clear()


class Select(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Select()
        self.ui.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.ui.pushButton.clicked.connect(self.show_fetchone)
        self.show()

    def show_fetchone(self):
        search_name = self.ui.lineEdit.text()
        conn = sqlite3.connect('./database/student.db')  # 必须使用绝对路径
        cursor = conn.cursor()
        query = "SELECT * FROM score WHERE 姓名=?"
        cursor.execute(query, (search_name,))
        row = cursor.fetchone()
        conn.commit()
        self.ui.textEdit.append(str(row))
        self.ui.lineEdit.clear()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Log()  # 显示Log类内容
    sys.exit(app.exec_())
